package isssr.boundary.src;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

import isssr.control.src.SuperOwnerSelectionControl;
import isssr.entity.src.UserDTO;

import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;


@ManagedBean(name="superOwnerSelectionBoundary")
@ViewScoped
public class SuperOwnerSelectionBoundary extends LazyDataModel<UserDTO> {

	private static final long serialVersionUID = 3023137915798130461L;
	private List<UserDTO> superOwnerList;
	
	@ManagedProperty(value="#{superOwnerSelectionControl}")
	private SuperOwnerSelectionControl ctrl;
	
	
	public SuperOwnerSelectionBoundary() {
		System.out.println("created SuperOwnerSelectionBoundary  ");
		this.superOwnerList = new ArrayList<UserDTO>(); 

	}
	
	
	public void setCtrl(SuperOwnerSelectionControl ctrl) {
		System.out.println("set SuperOwnerSelectionControl  ");
		this.ctrl = ctrl;
	}


	public List<UserDTO> getSuperOwnerList() {
		return superOwnerList;
	}

	
	public void setSuperOwnerList(List<UserDTO> superOwnerList) {
		this.superOwnerList = superOwnerList;
	}


	public void addSuperOwnerToList(UserDTO toAdd) {
		if(toAdd != null)
		{
			if(!this.superOwnerList.contains(toAdd))
				this.superOwnerList.add(toAdd);
		}
	}

	
	public void removeSuperOwnerToList(UserDTO toRemove) {
		if(toRemove != null)
			this.superOwnerList.remove(toRemove);
	}
	
/*
	public void confirmSelection() {
		this.ctrl.saveCurrentSelection(this.superOwnerList);
	}
	
	
	public void cancelSelection() {
		this.superOwnerList = this.ctrl.loadCurrentSelection();
	}
	*/
	
	@Override
	public void setRowIndex(int rowIndex) {
	    /*
	     * The following is in ancestor (LazyDataModel):
	     * this.rowIndex = rowIndex == -1 ? rowIndex : (rowIndex % pageSize);
	     */
	    if (rowIndex == -1 || getPageSize() == 0) {
	        super.setRowIndex(-1);
	    }
	    else
	        super.setRowIndex(rowIndex % getPageSize());
	}
	

	@Override
	public List<UserDTO> load(int first, int pageSize, String sortField,
			SortOrder sortOrder, Map<String, String> filters) {
		
		List<UserDTO> resultList = new ArrayList<UserDTO>();
		int resultNumber = 0; 
		
		if(this.ctrl != null)
			resultNumber = this.ctrl.loadElegibleSuperOwner(first, pageSize, sortField, sortOrder, filters, resultList);
		else
			System.out.println("ctr NULL!!!!!!!");
		this.setRowCount(resultNumber); 
		return resultList;
	}

}
